﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Odbc;
using System.ComponentModel;



namespace SocketServerMelo
{
    public class ConexaoOdbc
    {
        public OdbcConnection DbConnection;
        private string nomeConexao = "TeAmoProfessorMelo";
        #region abrir conexao
        public void abrirConexao()
        {
            this.DbConnection = new OdbcConnection("DSN=" + nomeConexao);
            try
            {
                this.DbConnection.Open();
            }
            catch (Exception ex)
            {
                throw new Exception("Falha ao conectar via ODBC " + ex.Message);
            }

        }
        #endregion
        #region fechar conexão
        public void fecharConexao()
        {
            this.DbConnection.Close();
        }
        #endregion






        public List<String> select(string nome)
        {
            List<String> retorno = new List<String>();
            try
            {
                this.abrirConexao();
                OdbcCommand DbCommand = DbConnection.CreateCommand();
                string sql = "SELECT nome FROM funcionario ";
                
                if(nome != ""){
                    sql += " where nome like '%" + nome + "%'";
                }
                DbCommand.CommandText = sql;
                    OdbcDataReader DbReader = DbCommand.ExecuteReader();
                while (DbReader.Read())
                {
                    string m =DbReader.GetString(DbReader.GetOrdinal("nome"));
                    retorno.Add(m);
                }
                
                DbReader.Close();
                DbCommand.Dispose();
                this.fecharConexao();
            }
            catch (Exception e)
            {

                throw new Exception("Falha ao executar instrução select " + e.Message);
            }
            return retorno;
        }

    }
}
